package com.module.product.manager;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.Transaction;

import base.common.CUDEnum;
import base.dao.GenericHibernateDao;
import base.db.HibernateSessionFactory;
import base.manager.GenericHibernateManager;
import base.util.Page;

import com.module.product.dao.AttrGroupDao;
import com.module.product.vo.AttrGroup;
/**
*
* 商品属性组业务处理类
*
* @author  王勇云
* @version  [V1.0, Jul 22, 2011]
* @see  [相关类/方法]
* @since  [二代服务平台/商品属性组]
*/
public class AttrGroupManager extends GenericHibernateManager<AttrGroup, String>{
	private static final AttrGroupManager instance = new AttrGroupManager();
	
	static Logger logger = Logger.getLogger(AttrGroupManager.class.getName());
	private AttrGroupManager(){}
	
	public synchronized static AttrGroupManager getInstance() {
		return instance;
	}
	
    AttrGroupDao dao = AttrGroupDao.getInstance();
    
    
    public boolean add(AttrGroup obj) throws Exception
    {
        if(StringUtils.isBlank(obj.getId())){//添加
            obj.setStatus(CUDEnum.C.toString());
            obj.setCreateTime(new Date());
        }else{
            obj.setStatus(CUDEnum.U.toString());
            obj.setUpdateTime(new Date());
        }
        if(obj.getAttrNum()==null){
            obj.setAttrNum("0");
        }
        return saveOrUpdate(obj);
    }
    public boolean delete(AttrGroup obj) throws Exception
    {
    	Session session =HibernateSessionFactory.getSession();
    	Transaction tx =session.beginTransaction();
    	boolean flag = false;
    	try{
	    		flag = dao.deleteObjs(session,obj);
	    		tx.commit();
	    	}catch(Exception e){
    		  if (tx != null) {
                  tx.rollback();
              }
	    		throw e;
	    	}finally{
	    		this.closeSession();
	    	}
        return flag;
    }
    public boolean update(AttrGroup obj) throws Exception
    {
    	Session session = HibernateSessionFactory.getSession();
    	boolean flag = false;
    	try{
    		flag =  dao.update(session,obj);
    	}catch(Exception e){
    		throw e;
    	}finally{
    		this.closeSession();
    	}
    	return flag;
    }
    
//    public AttrGroup getAttGroup(String id)
//    {
//        return get(id);
//    }
    
    public Page getList(AttrGroup obj,Page page) throws Exception{
    	Session session =HibernateSessionFactory.getSession();
    	Page pagePro = null;
    	try{
    	    	pagePro  = dao.getList(session,obj,page);	
	    	}catch(Exception e){
	    		throw e;
 
	    	}finally{
	    		this.closeSession();
	    	}
        return pagePro;
    }
    
    public List<AttrGroup> listAll() throws Exception{
    	Session session =HibernateSessionFactory.getSession();
    	 List<AttrGroup> list  = new ArrayList();
    	try{
    		list = dao.listAll(session);
	    	}catch(Exception e){
	    		throw e;

	    	}finally{
	    		this.closeSession();
	    	}
        return list;
    }
    
    public List<AttrGroup> getAttrGroupRootList(String pid) throws Exception{
    	Session session =HibernateSessionFactory.getSession();
    	 List<AttrGroup> list = null ;
    	try{
    	    	list = dao.getAttrGroupRootList(session,pid);	
	    	}catch(Exception e){
	    		throw e;
	    	}finally{
	    		this.closeSession();
	    	}
        return list;
    }
    
    public List <AttrGroup> getAttrGroupTreeOne(String id) throws Exception
    {
    	 Session session =HibernateSessionFactory.getSession();
    	 List<AttrGroup> list = null;
    	 try{
    	     	list = dao.getAttrGroupTreeOne(session,id);
	     	}catch(Exception e){
	     		throw e;
	     	}finally{
	     		this.closeSession();
	     	}
    	return list;
    }
    /**
     * 根据属性组名和类别id查找属性组
     * @param obj
     * @return
     * @throws Exception
     */
    public AttrGroup findAttrGroupByName(AttrGroup obj)throws Exception{
    	Session session = this.getSession();
    	 AttrGroup attrGroup=dao.findAttrGroupByName(session,obj.getCategoryId(),obj.getName());
    	 return attrGroup;
    }

	@Override
	public GenericHibernateDao<AttrGroup, String> getDao() {
		// TODO Auto-generated method stub
		return dao;
	}
	/**
	 * 根据categoryId统计该类别下的属性组数量
	 * @return
	 * @throws Exception
	 */
	public List getCateAttrGroupList() throws Exception{
    	Session session =HibernateSessionFactory.getSession();
    	 List list  = new ArrayList();
    	try{
    		list = dao.getCateAttrGroupList(session);
	    	}catch(Exception e){
	    		e.printStackTrace();

	    	}finally{
	    		this.closeSession();
	    	}
        return list;
    }
    
}
